<?php
namespace app\jok\logic;
use app\common\model\systemAuth;
use app\common\logic\systemAuthLogic as commonAuthLogic;
use app\common\validate\SystemAuth as SystemAuthValidate;
class systemAuthLogic extends jokCommonLogic
{
    public function authGetList($param){
        $result = (new commonAuthLogic)->getAuthList(0,1);
        return ['code'=>1,'msg'=>'获取成功','data'=>$result];
    }
    public function authAddEdit($param){
        $systemAuth = new systemAuth;
        $check = new SystemAuthValidate;
        if(true !== $check->check($param)){
            return ['code'=>0,'msg'=>$check->getError()];
        }
        $level = $systemAuth->where(['id'=>$param['pid']])->value('level');
        if(!$level){
            return ['code'=>0,'msg'=>'未知错误'];
        }
        $param['level'] = $level + 1;
        if(!empty($param['id'])){
            $result = $systemAuth->allowField(true)->isUpdate(true)->save($param);
            $title = '编辑';
        }else{
            unset($param['id']);
            $result = $systemAuth->allowField(true)->save($param);
            $title = '新增';
        }
        if($result){
            return ['code'=>1,'msg'=>$title.'成功'];
        }else{
            return ['code'=>0,'msg'=>$title.'失败'];
        }

    }
    public function authDataDetail($param){
        if(empty($param['id'])){
            return ['code'=>0,'msg'=>'参数错误'];
        }
        $fun = function($query)use($param){
            $query->field('id,pid,alias,name,status,icon')->where(['id'=>$param['id']]);
        };
        $result = systemAuth::get($fun);
        return ['code'=>1,'msg'=>'获取成功','data'=>$result];
    }
}